import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

# 初始化驱动
driver = webdriver.Chrome()

# 打开百度
driver.get('https://www.baidu.com/')

# 最大化浏览器
driver.maximize_window()

# 点击设置
WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((By.NAME,'tj_settingicon')))[1].click()

# 手写XPATH定位：搜索设置按钮
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[text()="搜索设置"]'))).click()
"""
手写xpath： //*表示从所有的层级开始寻找，text()='搜索设置'，找到文本内容是搜索设置的xpath元素
"""
# 等待弹窗弹出
time.sleep(1)
# 点击保存设置
WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//*[text()="保存设置"]'))).click()
time.sleep(2)
# 捕捉alert
alert = driver.switch_to.alert      # 获取当前的alert弹窗

# 点击弹窗中的确认
alert.accept()

# 点击弹窗中的取消
# alert.dismiss()

# 加一个等待，看页面是否通过
time.sleep(5)



